package cn.com.hhrcw.utils;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author sunchao
 */
@Slf4j
public class ProvinceCityCountyUtils {


    /**
     * 获取省市县简称
     * @param homeTown
     * @return
     */
    public static String getProvinceCityCountyShort(List<String> homeTown){
        String province = "";//省
        String city = "";//市
        String county = "";//县
        int homeTownLen = homeTown.size();
        if(homeTownLen > 0){
            province = homeTown.get(0);
        }
        if(homeTownLen > 1){
            city = homeTown.get(1);
        }
        if(homeTownLen > 2){
            county = homeTown.get(2);
        }
        String cityOrCounty = "";
        //直辖市和香港澳门
        if(province.equals(city)){
            if(county.length() > 2){
                cityOrCounty = county.replace("区","").
                        replace("新区","");
            }else{
                cityOrCounty = county;
            }
        }else{
            if(county.contains("市") || county.contains("县")){
                if(county.length() > 2){
                    cityOrCounty = ProvinceCityCountyUtils.getCountyAbbreviation(county);
                }else{
                    cityOrCounty = county;
                }
            }else{
                if(city.length() > 2){
                    cityOrCounty = ProvinceCityCountyUtils.getCityAbbreviation(city);
                }else{
                    cityOrCounty = city;
                }
            }
        }
        //省份处理
        province = ProvinceCityCountyUtils.getProvinceAbbreviation(province);
        return province.concat(cityOrCounty);
    }

    /***
     * 获取省份简称
     * @param provinceName
     * @return
     */
    public static String getProvinceAbbreviation(String provinceName){
        if(provinceName.contains("省")){
            provinceName = provinceName.replace("省","");
        }else if(provinceName.contains("市")){
            provinceName = provinceName.replace("市","");
        }else if(provinceName.contains("壮族自治区")){
            provinceName = provinceName.replace("壮族自治区","");
        }else if(provinceName.contains("回族自治区")){
            provinceName = provinceName.replace("回族自治区","");
        }else if(provinceName.contains("维吾尔自治区")){
            provinceName = provinceName.replace("维吾尔自治区","");
        }else if(provinceName.contains("自治区")){
            provinceName = provinceName.replace("自治区","");
        }else if(provinceName.contains("特别行政区")){
            provinceName = provinceName.replace("特别行政区","");
        }
        return provinceName;
    }

    /***
     * 获取市简称
     * @param cityName
     * @return
     */
    public static String getCityAbbreviation(String cityName){
        if(cityName.contains("市")){
            cityName = cityName.replace("市","");
        }else if(cityName.contains("盟")){
            cityName = cityName.replace("盟","");
        }else if(cityName.contains("地区")){
            cityName = cityName.replace("地区","");
        }else if(cityName.contains("黎族自治县")){
            cityName = cityName.replace("黎族自治县","");
        }else if(cityName.contains("黎族苗族自治县")){
            cityName = cityName.replace("黎族苗族自治县","");
        }else if(cityName.contains("县")){
            cityName = cityName.replace("县","");
        }
        return cityName;
    }

    /***
     * 获取县简称
     * @param countyName
     * @return
     */
    public static String getCountyAbbreviation(String countyName){
        if(countyName.contains("市")){
            countyName = countyName.replace("市","");
        }else if(countyName.contains("县") && (!countyName.contains("自治县"))){
            countyName = countyName.replace("县","");
        }else if(countyName.contains("满族自治县")){
            countyName = countyName.replace("满族自治县","");
        }else if(countyName.contains("满族蒙古族自治县")){
            countyName = countyName.replace("满族蒙古族自治县","");
        }else if(countyName.contains("彝族回族自治县")){
            countyName = countyName.replace("彝族回族自治县","");
        }else if(countyName.contains("回族自治县")){
            countyName = countyName.replace("回族自治县","");
        }else if(countyName.contains("蒙古族自治县")){
            countyName = countyName.replace("蒙古族自治县","");
        }else if(countyName.contains("朝鲜族自治县")){
            countyName = countyName.replace("朝鲜族自治县","");
        }else if(countyName.contains("畲族自治县")){
            countyName = countyName.replace("畲族自治县","");
        }else if(countyName.contains("土家族自治县")){
            countyName = countyName.replace("土家族自治县","");
        }else if(countyName.contains("仡佬族苗族自治县")){
            countyName = countyName.replace("仡佬族苗族自治县","");
        }else if(countyName.contains("布依族苗族自治县")){
            countyName = countyName.replace("布依族苗族自治县","");
        }else if(countyName.contains("彝族回族苗族自治县")){
            countyName = countyName.replace("彝族回族苗族自治县","");
        }else if(countyName.contains("土家族苗族自治县")){
            countyName = countyName.replace("土家族苗族自治县","");
        }else if(countyName.contains("彝族苗族自治县")){
            countyName = countyName.replace("彝族苗族自治县","");
        }else if(countyName.contains("苗族自治县")){
            countyName = countyName.replace("苗族自治县","");
        }else if(countyName.contains("壮族瑶族自治县")){
            countyName = countyName.replace("壮族瑶族自治县","");
        }else if(countyName.contains("瑶族自治县")){
            countyName = countyName.replace("瑶族自治县","");
        }else if(countyName.contains("苗族侗族自治县")){
            countyName = countyName.replace("苗族侗族自治县","");
        }else if(countyName.contains("侗族自治县")){
            countyName = countyName.replace("侗族自治县","");
        }else if(countyName.contains("各族自治县")){
            countyName = countyName.replace("各族自治县","");
        }else if(countyName.contains("仫佬族自治县")){
            countyName = countyName.replace("仫佬族自治县","");
        }else if(countyName.contains("毛南族自治县")){
            countyName = countyName.replace("毛南族自治县","");
        }else if(countyName.contains("羌族自治县")){
            countyName = countyName.replace("羌族自治县","");
        }else if(countyName.contains("回族彝族自治县")){
            countyName = countyName.replace("回族彝族自治县","");
        }else if(countyName.contains("哈尼族彝族自治县")){
            countyName = countyName.replace("哈尼族彝族自治县","");
        }else if(countyName.contains("傣族彝族自治县")){
            countyName = countyName.replace("傣族彝族自治县","");
        }else if(countyName.contains("彝族自治县")){
            countyName = countyName.replace("彝族自治县","");
        }else if(countyName.contains("水族自治县")){
            countyName = countyName.replace("水族自治县","");
        }else if(countyName.contains("纳西族自治县")){
            countyName = countyName.replace("纳西族自治县","");
        }else if(countyName.contains("苗族瑶族傣族自治县")){
            countyName = countyName.replace("苗族瑶族傣族自治县","");
        }else if(countyName.contains("彝族傣族自治县")){
            countyName = countyName.replace("彝族傣族自治县","");
        }else if(countyName.contains("哈尼族彝族傣族自治县")){
            countyName = countyName.replace("哈尼族彝族傣族自治县","");
        }else if(countyName.contains("拉祜族佤族布朗族傣族自治县")){
            countyName = countyName.replace("拉祜族佤族布朗族傣族自治县","");
        }else if(countyName.contains("哈尼族自治县")){
            countyName = countyName.replace("哈尼族自治县","");
        }else if(countyName.contains("彝族哈尼族拉祜族自治县")){
            countyName = countyName.replace("彝族哈尼族拉祜族自治县","");
        }else if(countyName.contains("拉祜族自治县")){
            countyName = countyName.replace("拉祜族自治县","");
        }else if(countyName.contains("傣族拉祜族佤族自治县")){
            countyName = countyName.replace("傣族拉祜族佤族自治县","");
        }else if(countyName.contains("傣族佤族自治县")){
            countyName = countyName.replace("傣族佤族自治县","");
        }else if(countyName.contains("佤族自治县")){
            countyName = countyName.replace("佤族自治县","");
        }else if(countyName.contains("独龙族怒族自治县")){
            countyName = countyName.replace("独龙族怒族自治县","");
        }else if(countyName.contains("白族普米族自治县")){
            countyName = countyName.replace("白族普米族自治县","");
        }else if(countyName.contains("僳族自治县")){
            countyName = countyName.replace("僳族自治县","");
        }else if(countyName.contains("藏族自治县")){
            countyName = countyName.replace("藏族自治县","");
        }else if(countyName.contains("裕固族自治县")){
            countyName = countyName.replace("裕固族自治县","");
        }else if(countyName.contains("裕固族自治县")){
            countyName = countyName.replace("裕固族自治县","");
        }else if(countyName.contains("保安族东乡族撒拉族自治县")){
            countyName = countyName.replace("保安族东乡族撒拉族自治县","");
        }else if(countyName.contains("回族土族自治县")){
            countyName = countyName.replace("回族土族自治县","");
        }else if(countyName.contains("土族自治县")){
            countyName = countyName.replace("土族自治县","");
        }else if(countyName.contains("哈萨克自治县")){
            countyName = countyName.replace("哈萨克自治县","");
        }else if(countyName.contains("塔吉克自治县")){
            countyName = countyName.replace("塔吉克自治县","");
        }else if(countyName.contains("锡伯自治县")){
            countyName = countyName.replace("锡伯自治县","");
        }else if(countyName.contains("族自治县")){
            countyName = countyName.replace("族自治县","");
        }

        return countyName;
    }

    /**
     * 家庭住址 去重
     * @return
     */
    public static String getLiveCity(String liveCity,String address){
        String homeAddress = "";
        if(StringUtil.isEmpty(liveCity)|| StringUtil.isEmpty(address)){
            homeAddress = liveCity.concat(address);
        }else{
            //取字符串最小值做循环
            int minLen = Math.min(liveCity.length(),address.length());
            //重复字段
            String repeatStr = "";
            for(int i = 0; i < minLen; i++){
                String liveCity_temp = liveCity.substring(liveCity.length()-1-i,liveCity.length());
                String address_temp = address.substring(0,i+1);
                if(liveCity_temp.equals(address_temp)){
                    repeatStr = address_temp;
                    break;
                }
            }
            homeAddress = liveCity.concat(" ").concat(address.replace(repeatStr,""));
        }
        return homeAddress;
    }
}
